//
//  CTLoginView.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/4/20.
//  Copyright (c) 2015年 hanwei. All rights reserved.
//

#import "CTLoginView.h"
#import "CTAppLanguage.h"
#import "UIViewAdditions.h"
#import "CTDebugManager.h"
#import "CTHeader.h"
#import "CTDeviceFunction.h"
#import "CTLoginView_Def.h"

#define CT_LOGINVIEW_TEXTFIELD_USERNAME_TAG 1000
#define CT_LOGINVIEW_TEXTFIELD_PASSWORD_TAG 1001

@interface CTLoginView ()<UITextFieldDelegate>
{
    UILabel *describeLabel;
    UITextField *userNameTextField;
    UITextField *passWordTextField;
}

@property (nonatomic, strong) UIView *logInView;
@end

@implementation CTLoginView


@synthesize delegate,logInView;

- (void)dealloc
{
    logInView = nil;
    [describeLabel removeFromSuperview];
    describeLabel = nil;
    [userNameTextField removeFromSuperview];
    userNameTextField = nil;
    [passWordTextField removeFromSuperview];
    passWordTextField = nil;
     [CTDebugManager showDebugLogInfo:@"dealloc : CTLoginView Is Removed!!!"];
}

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        self.backgroundColor = [UIColor clearColor];
        self.frame = CGRectMake(0, 0,CT_HEADER_SCREEN_WIDTH,CT_HEADER_SCREEN_HEIGHT);
        self.backgroundColor = CT_HEADER_WHITE_COLOR;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tpaAction:)];
        [self addGestureRecognizer:tap];
        
        [self createLogInTitleView];
        [self createLogInView];

    }
    return self;
}
//标题
- (void)createLogInTitleView
{
    //返回按钮
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setImage:[UIImage imageNamed:@"quxiao"] forState:UIControlStateNormal];
    [backButton setImage:[UIImage imageNamed:@"quxiao_click"] forState:UIControlStateHighlighted];
    backButton.frame = CGRectMake(0, 20, 44, 44);
    [backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:backButton];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,0.1199*CT_HEADER_SCREEN_HEIGHT , CT_HEADER_SCREEN_WIDTH, 30)];
    titleLabel.text = [CTAppLanguage localizedStringForKey:@"logInView_titleLabel_text" withValue:nil withTable:@"Language"];
    titleLabel.textColor = CT_HEADER_GREEN_COLOR;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:titleLabel];
    DeviceTypeAdaptation deviceType = [CTDeviceFunction deviceTypeForResolutionAdaptation];
    if (deviceType == iPhone4_4s)
    {
        titleLabel.font = [UIFont fontWithName:nil size:27];

    }
    else if (deviceType == iPhone5_5s_5c)
    {
        titleLabel.font = [UIFont fontWithName:nil size:27];

    }
    else
    {
        titleLabel.font = [UIFont fontWithName:nil size:30];

    }
    
    describeLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, titleLabel.bottom+15, CT_HEADER_SCREEN_WIDTH, 10)];
    describeLabel.text = [CTAppLanguage localizedStringForKey:@"logInView_describeLabel_text" withValue:nil withTable:@"Language"];
    describeLabel.font = [UIFont fontWithName:nil size:LOGINVIEW_DESCRIBE_FONTSIZE];
    describeLabel.textColor = LOGINVIEW_DESCRIBE_COLOR;
    describeLabel.textAlignment = NSTextAlignmentCenter;
//    [self addSubview:describeLabel];
}
//登录View
- (void)createLogInView
{
    DeviceTypeAdaptation deviceType = [CTDeviceFunction deviceTypeForResolutionAdaptation];
    if (deviceType == iPhone4_4s)
    {
        logInView =[[UIView alloc]initWithFrame:CGRectMake((CT_HEADER_SCREEN_WIDTH-225)/2, describeLabel.bottom+20, 225, 190)];
        [self addSubview:logInView];
    }
    else if(deviceType == iPhone5_5s_5c)
    {
        logInView =[[UIView alloc]initWithFrame:CGRectMake((CT_HEADER_SCREEN_WIDTH-225)/2, describeLabel.bottom+30, 225, 190)];
        [self addSubview:logInView];

    }
    else
    {
        logInView =[[UIView alloc]initWithFrame:CGRectMake((CT_HEADER_SCREEN_WIDTH-225)/2, describeLabel.bottom+0.1199*CT_HEADER_SCREEN_HEIGHT, 225, 190)];
        [self addSubview:logInView];
    }
   
    //输入手机号
    UIImageView *userImagerView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 15, 16)];
    userImagerView.image = [UIImage imageNamed:@"login_icon_username"];
    [logInView addSubview:userImagerView];
    
    userNameTextField = [[UITextField alloc]initWithFrame:CGRectMake(userImagerView.right, 1, 210, 15)];
    userNameTextField.delegate = self;
    userNameTextField.textColor = CT_HEADER_DEEPLINE_COLOR;
    userNameTextField.font = [UIFont fontWithName:nil size:15];
    userNameTextField.textAlignment = NSTextAlignmentCenter;
    userNameTextField.keyboardType = UIKeyboardTypePhonePad;
    userNameTextField.tag = CT_LOGINVIEW_TEXTFIELD_USERNAME_TAG;
    userNameTextField.placeholder = [CTAppLanguage localizedStringForKey:@"logInView_userNameTextField" withValue:nil withTable:@"Language"];
    [logInView addSubview:userNameTextField];
    
    UIImageView *userLabel = [[UIImageView alloc]initWithFrame:CGRectMake(0, userNameTextField.bottom+10, 225, 0.5)];
    userLabel.image = [UIImage imageNamed:@"login_line_username"];
    [logInView addSubview:userLabel];
    //输入密码
    UIImageView *passWordImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, userLabel.bottom+33, 15, 16)];
    passWordImageView.image = [UIImage imageNamed:@"login_icon_password"];
    [logInView addSubview:passWordImageView];
    
    passWordTextField = [[UITextField alloc]initWithFrame:CGRectMake(passWordImageView.right, userLabel.bottom+30, 210, 20)];
    passWordTextField.delegate = self;
    passWordTextField.secureTextEntry = YES;
    passWordTextField.font = [UIFont systemFontOfSize:15.0f];
    passWordTextField.textColor = CT_HEADER_DEEPLINE_COLOR;
    passWordTextField.keyboardType = UIKeyboardTypeDefault;
    passWordTextField.textAlignment = NSTextAlignmentCenter;
    passWordTextField.tag = CT_LOGINVIEW_TEXTFIELD_PASSWORD_TAG;
    passWordTextField.placeholder = [CTAppLanguage localizedStringForKey:@"logInView_passWordTextField" withValue:nil withTable:@"Language"];
    [logInView addSubview:passWordTextField];
    
    UIImageView *passWordLabel = [[UIImageView alloc]initWithFrame:CGRectMake(0, passWordTextField.bottom+10, 225, 0.5)];
    passWordLabel.image = [UIImage imageNamed:@"login_line_username"];
    [logInView addSubview:passWordLabel];
    
    //登陆按钮
    UIButton *logInButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    DeviceTypeAdaptation deviceType = [CTDeviceFunction deviceTypeForResolutionAdaptation];
    if (deviceType == iPhone5_5s_5c)
    {
        logInButton.frame = CGRectMake(0, passWordLabel.bottom+29, 226, 38);

    }
    else if(deviceType == iPhone4_4s)
    {
        logInButton.frame = CGRectMake(0, passWordLabel.bottom+29, 226, 38);

    }
    else
    {
        logInButton.frame = CGRectMake(0, passWordLabel.bottom+34, 226, 38);

    }
    [logInButton addTarget:self action:@selector(logInButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [logInButton setTitle:[CTAppLanguage localizedStringForKey:@"logInView_LoginButton" withValue:nil withTable:@"Language"] forState:UIControlStateNormal];
    [logInButton setTitle:[CTAppLanguage localizedStringForKey:@"logInView_LoginButton" withValue:nil withTable:@"Language"] forState:UIControlStateHighlighted];
    [logInButton setTitleColor:CT_HEADER_GREEN_COLOR forState:UIControlStateNormal];
    logInButton.layer.cornerRadius = 3;
    logInButton.layer.masksToBounds =YES;
    CALayer *btnlayer = [logInButton layer];
    btnlayer.borderColor = [CT_HEADER_GREEN_COLOR CGColor];
    btnlayer.borderWidth = 0.5f;
    [logInView addSubview:logInButton];
    
    //注册
    NSString *registerBtn_title = [CTAppLanguage localizedStringForKey:@"logInView_registerButton" withValue:nil withTable:@"Language"];
    UIButton *registerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    registerButton.frame = CGRectMake(-5, logInButton.bottom+20, registerBtn_title.length*15+10, 15);
    [registerButton setTitleColor:CT_HEADER_BLACK_COLOR forState:UIControlStateNormal];
    [registerButton setTitleColor:CT_HEADER_BLACK_COLOR forState:UIControlStateHighlighted];
    [registerButton addTarget:self action:@selector(registerButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    registerButton.titleLabel.font = [UIFont systemFontOfSize:12.0f];
    [registerButton setTitle: registerBtn_title forState:UIControlStateNormal];
    [logInView addSubview:registerButton];
    
    //忘记密码
    NSString *restPWB_title =[CTAppLanguage localizedStringForKey:@"logInView_forgetButton" withValue:nil withTable:@"Language"];
    
    UIButton *restPassWordButton = [UIButton buttonWithType:UIButtonTypeCustom];
    restPassWordButton.frame = CGRectMake(logInButton.width-restPWB_title.length*15, logInButton.bottom+20, restPWB_title.length*15+10, 15);
    [restPassWordButton addTarget:self action:@selector(restPassWordButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    restPassWordButton.titleLabel.textAlignment = NSTextAlignmentRight;
    restPassWordButton.titleLabel.font = [UIFont systemFontOfSize:12.0f];
    [restPassWordButton setTitleColor:CT_HEADER_BLACK_COLOR forState:UIControlStateHighlighted];
    [restPassWordButton setTitleColor:CT_HEADER_BLACK_COLOR forState:UIControlStateNormal];
    [restPassWordButton setTitle:restPWB_title forState:UIControlStateNormal];
    [logInView addSubview:restPassWordButton];
}
//返回
- (void)backButtonAction:(UIButton *) sender
{
    if ([self.delegate respondsToSelector:@selector(logInBackButton:)])
    {
        [self.delegate logInBackButton:self];
    }
}
//登陆
- (void)logInButtonAction:(UIButton *)sender
{
    [userNameTextField resignFirstResponder];
    [passWordTextField resignFirstResponder];
    if ([self.delegate respondsToSelector:@selector(logInButton:)])
    {
        [self.delegate logInButton:self];
    }
}
//注册
- (void)registerButtonAction:(UIButton *)sender
{
    if ([self.delegate respondsToSelector:@selector(registerButton:)])
    {
        [self.delegate registerButton:self];
    }
}
//忘记密码
- (void)restPassWordButtonAction:(UIButton *)sender
{
    if ([self.delegate respondsToSelector:@selector(restPassWordButton:)])
    {
        [self.delegate restPassWordButton:self];
    }
}
#pragma mark TextFieldDelegate

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{

    return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    switch (textField.tag) {
        case CT_LOGINVIEW_TEXTFIELD_USERNAME_TAG:
            if ([self.delegate respondsToSelector:@selector(logInUserNameTextFieldText:withView:)])
                [self.delegate logInUserNameTextFieldText:textField.text withView:self];
            break;
        case CT_LOGINVIEW_TEXTFIELD_PASSWORD_TAG:
            if ([self.delegate respondsToSelector:@selector(logInPassWordTextFieldText:withView:)])
                [self.delegate logInPassWordTextFieldText:textField.text withView:self];
            break;
        default:
            break;
    }
}
//收回键盘
- (void)tpaAction:(UITapGestureRecognizer *) sender
{
    [userNameTextField resignFirstResponder];
    [passWordTextField resignFirstResponder];
}
@end
